package com.anolesoft.yr.store.stk.ctrl;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.core.web.page.TableDataInfo;
import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.yr.store.stk.dto.StkMainDTO;
import com.anolesoft.yr.store.stk.entity.StkMain;
import com.anolesoft.yr.store.stk.entity.StkMain;
import com.anolesoft.yr.store.stk.entity.StkMainExample;
import com.anolesoft.yr.store.stk.entity.StkType;
import com.anolesoft.yr.store.stk.service.StkMainService;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.anolesoft.yr.store.stk.vo.StkMainVO;
import com.anolesoft.yr.store.stk.vo.StkTypeVO;
import com.anolesoft.yr.util.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/15 19:33
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/store/stk/stkmain")
public class StkMainController {
    private StkMainService stkMainService;
    private IEmployeeService iEmployeeService;

    @GetMapping("/list/combox")
    public AjaxResult listCombox(StkMainVO stkMainVO){
        LambdaQueryWrapper<StkMain> like = new QueryWrapper<StkMain>()
                .lambda()
                .eq(StkMain::getDelFlag,0)
                ;
        if(!StringUtils.isEmpty(stkMainVO.getStkName())){
            like.like(StkMain::getStkName, stkMainVO.getStkName());
        }

        Page<StkMain> page = new Page<>(stkMainVO.getPage(), stkMainVO.getRows());
        List<StkMainDTO> stkMains = stkMainService.pageUnion(page, like);
        long total = page.getTotal();
        List<StkMainVO> vos = stkMains.stream().map(CustomBeanUtils::stkMainDTO2VO).collect(Collectors.toList());
        PageInfo<List<StkMainVO>> pageInfo = new PageInfo<>((int) total, vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @Transactional
    @RequestMapping(value = "/get", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(StkMain stkMain) {
        Page<StkMain> page = new Page<>(stkMain.getPage(), stkMain.getRows());
        String stkName = StrUtil.nullToEmpty(stkMain.getStkName());
        LambdaQueryWrapper<StkMain> like = new QueryWrapper<StkMain>()
                .lambda()
                .like(StkMain::getStkName, stkName)
                .orderByDesc(StkMain::getcTime);

        Page<StkMain> result = stkMainService.page(page, like);
        LambdaQueryWrapper<Employee> emplike = new QueryWrapper<Employee>()
                .lambda();
        List<Employee> employees = iEmployeeService.getBaseMapper().selectList(emplike);
        Map<String, String> collect = employees.stream().collect(Collectors.toMap(Employee::getObjuid, Employee::getEmpName));


        List<StkMain> records = result.getRecords();

        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).getcUserUid() != null) {
                records.get(i).setcUserUid(collect.get(records.get(i).getcUserUid()));
            }
        }
        return AjaxResult.success(new TableDataInfo(result.getTotal(), records));
    }

    @RequestMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public AjaxResult del(StkMain stkMain) {

        stkMainService.removeById(stkMain.getObjuid());
        return AjaxResult.success("删除成功");
    }

    @Transactional
    @RequestMapping(value = "/delBatch", produces = {"application/json;charset=UTF-8"})
    public AjaxResult delBatch(@RequestBody List<StkMain> stkTypes) {
        stkMainService.removeByIds(stkTypes.stream().map(StkMain::getObjuid).collect(Collectors.toList()));
        return AjaxResult.success("删除成功");
    }

    @Transactional
    @RequestMapping(value = "/edit", produces = {"application/json;charset=UTF-8"})
    public AjaxResult edit(StkMain stkMain) {
        String empUid = SecurityUtils.getLoginUser().getEmpUid();
        stkMain.setmUserUid(empUid);
        stkMain.setmTime(new Date());
        stkMainService.updateById(stkMain);
        return AjaxResult.success("修改成功");
    }

    @Transactional
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public AjaxResult add(StkMain stkMain) {
        StkMainExample stkMainExample = new StkMainExample();
        stkMainExample.createCriteria().andStkNameEqualTo(stkMain.getStkName());
        List<StkMain> stkTypes = stkMainService.selectByExample(stkMainExample);
        if (stkTypes.isEmpty()) {
            String empUid = SecurityUtils.getLoginUser().getEmpUid();
            stkMain.setcUserUid(empUid);
            stkMain.setcTime(new Date());
            stkMain.setObjuid(StringUtil.generateRundom32String());
            stkMainService.save(stkMain);
            return AjaxResult.success("保存成功");
        } else {
            return AjaxResult.success("保存失败，类型名不可以重复");
        }
    }

    @RequestMapping(value = "/unique", produces = {"application/json;charset=UTF-8"})
    public boolean unique(String stkMain, String objuid) {
        objuid = StrUtil.nullToEmpty(objuid);
        StkMainExample stkMainExample = new StkMainExample();
        stkMainExample.createCriteria().andStkNameEqualTo(stkMain).andObjuidNotEqualTo(objuid);
        List<StkMain> stkTypes = stkMainService.selectByExample(stkMainExample);
        if (!stkTypes.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    @RequestMapping(value = "/getById")
    public AjaxResult getById(@RequestBody StkMain stkMain) {
        StkMain result = stkMainService.getById(stkMain.getObjuid());
        return AjaxResult.success(result);
    }

}
